home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11572 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  145 lines

  1. Path: news.mindspring.com!usenet
  2. From: rudd@mindspring.com (Justin Rudd)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ Shortcomings ?
  5. Date: Fri, 15 Mar 1996 04:05:17 GMT
  6. Organization: MindSpring Enterprises
  7. Message-ID: <4iaqch$moh@B1FF.mindspring.com>
  8. References: <31488E8D.167E@aw.sgi.com>
  9. Reply-To: rudd@mindspring.com
  10. NNTP-Posting-Host: rudd.mindspring.com
  11. X-Newsreader: Forte Free Agent v0.55
  12.  
  13. Emmanuel Mogenet <mgix@aw.sgi.com> wrote:
  14.  
  15. >In other word, make portions of the interface to a class private to
  16. >only some classes.
  17.  
  18. >        1. Wouldn't that be nicer than the friend mechanism that cracks
  19. >           open a class a spill its guts ?
  20. >        2. Is there a clean way to achieve in today's C++ standard ?
  21. >        3. Or is it a bad idea altogether ?
  22.  
  23.  
  24. I'm not sure what you mean by this...You want to have a function
  25. SomeMethodOnlyCallableByManagerForClassA();  That can be called like
  26. this:
  27.  
  28. ManagerForClassA manage;
  29. manage.SomeMethodOnlyCallableByManagerForClassA();
  30.  
  31. hm...OH WAIT...I get it....
  32.  
  33. even though ManagerForClassA has no idea what it has inside it should
  34. still be able to call a member function of A.
  35.  
  36. Well this could be simulate using inheritance.  Hm...I've never really
  37. thought about this...let me get back to you on this one.
  38.  
  39. >2. Pointer type manipulation
  40. >-----------------------------
  41.  
  42. >A very disappointing thing in C++ (unless I am mistaken and it is actually
  43. >possible to do so) is the following situation:
  44.  
  45. >If A is a class, then most operations on A can be redefined.
  46. >Because A is a full blown type.
  47.  
  48. >Sadly, the same can not be said about A* (type: pointer to A).
  49.  
  50. >Even though A* is a type, none of its default manipulations
  51. >can be redefined.
  52.  
  53. >Example: You can tell C++, that you want to gain control of the
  54. >situation whenever an object of type A is duplicated.
  55.  
  56. >You do so by redefining the default copy constructor and the default
  57. >assignment operator.
  58.  
  59. >But can you tell C++ that you want to gain control whenever a pointer of
  60. >A* is duplicated ? Why can't I redefine the copy constructor for the type A* ?
  61.  
  62. >In my way of looking at thing, that'd be a *great* way of doing clean reference
  63. >counting, instead of half-baked method using a redefinition of operator->.
  64.  
  65. >Comments ?
  66.  
  67. Ok...so you are saying you can do something like this...
  68.  
  69. A a1, a2;
  70.  
  71. a1 = a2;
  72.  
  73. But you want to be able to do this
  74.  
  75. A a1, *a2;
  76.  
  77. a1 = a2;
  78.  
  79. Well you can if you deference the pointer like this
  80.  
  81. a1 = *a2;  
  82.  
  83. But what you are suggesting although it would be nice sometimes is
  84. really impossible.  Because a2 points to a memory address that holds
  85. an object of type A.  You can't expect the compiler or linker to see
  86. this:
  87.  
  88. A a1, *a2;
  89.  
  90. a1 = a2;
  91.  
  92. and go look at the memory address and determine it is a location that
  93. holds an object of type A and then do the regular assignment.  Like I
  94. said before it would be nice but shouldn't be done.  Because what
  95. happens if I have this
  96.  
  97. class Graphics
  98. {
  99.     public:
  100.         virtual void Draw() = 0;
  101. };
  102.  
  103. class Box
  104. {
  105.     public:
  106.         virtual void Draw();
  107. };
  108.  
  109. class Line
  110. {
  111.     public:
  112.         virtual void Draw();
  113. };
  114.  
  115. void main()
  116. {
  117.     Graphics* graph;
  118.     Box box1, box2;
  119.     Line line1, line2;
  120.  
  121.     graph = &box1;
  122.     graph->Draw();
  123.     
  124.     box2 = graph;    //what you want is this to assign box1 to box2
  125.  
  126.     graph = &line1;
  127.     graph->Draw();
  128.  
  129.     box2 = graph;    //before this worked but now it won't because it is
  130.             // a line object.  If you build this kind of checking into
  131.             // the language you will severly slow it down.
  132. }
  133.  
  134. Like I said in the comments you will slow the speed of your program
  135. down because of this type of checking and since these are virtual
  136. functions it will be slowed down even more because its not determined
  137. till runtime.
  138.  
  139. I know I was probably rambling and I'll get flamed for some of the
  140. comments but...these are my comments and let it be known I speak my
  141. mind.
  142.  
  143. Justin
  144.  
  145.